home *** CD-ROM | disk | FTP | other *** search
- Path: news.uh.edu!sukku
- From: sukku@menudo.uh.edu (sukumar)
- Newsgroups: comp.lang.c
- Subject: Re: GoTo equivalent in C ??
- Date: 15 Jan 1996 17:53:18 GMT
- Organization: University of Houston
- Message-ID: <4de4ae$h6a@masala.cc.uh.edu>
- References: <4d67vm$e5h@masala.cc.uh.edu> <4d8sa6$mqc@news.iag.net>
- NNTP-Posting-Host: menudo.uh.edu
- X-Newsreader: TIN [version 1.2 PL2]
-
- John R Buchan (jatmon@iag.net) wrote:
- : In article <4d67vm$e5h@masala.cc.uh.edu>, sukku@menudo.uh.edu says...
- : >
- : >Hi,
- : > I have always thought about this. How do you get the effect of goto
- : >in C without using "goto"?? For ex, if I have to check for an error in my
- : >function for every computation I do, and then do some cleaning up, how do
- : >I do it. Here is an example:
- : >
- : >f()
- : >{
- : >
- : >char *s = (char *) malloc(10 * sizeof(char));
- : >int i;
- : >
- : >i = scanf("%s", s);
- : >
- : >if(i != 1) /* I wish I could do a goto to the last two lines of the
- : function*/
- : >{
- : > free(s);
- : > return;
- : >}
- : >
- : >i = atoi(s);
- : >if(i==0)
- : >{
- : > free(s);
- : > return;
- : >
- : >}
- : >free(s);
- : >return;
- : >
- : >}
-
- : f()
- : {
- : int i;
- : char *s = malloc(10 * sizeof(char));
-
- : if( s != NULL) /* allocation succeeeded */
- : {
- : if( (scanf("%s", s)) == 1) /* 1 field input */
- : {
- : if( (i = atoi(s)) != 0) /* converted to non-zero integer */
- : {
- : /* more code */
- : }
- : }
- : free(s);
- : }
- : return;
- : }
-
- : or
-
- : f()
- : {
- : int i;
- : char *s = malloc(10 * sizeof(char));
-
- : if( s != NULL) /* successful allocation */
- : {
- : if( ((scanf("%s", s)) == 1)) && ((i = atoi(s)) != 0) ) /* valid input */
- : {
- : /* more code */
- : }
- : free(s);
- : }
- : return;
- : }
-
- : Note: The && operator guarantees that the scanf test must succeed _before_
- : the atoi can be called.
-
- : --
- : John R Buchan -:|:- Looking for that elusive FAQ? ftp to:
- : jatmon@mail.iag.net -:|:- rtfm.mit.edu /pub/usenet-by-group/....
-
- The case is not as simple as this one always. Typically it is like this:
-
-
- (Alloc memory for a bunch of vars)
- ...
- for every computation, if error flagged, clean up and return
-
-
- This could be achieved by using one error flag like this:
-
- int flag =0;
-
-
- If(computaion fails)
- flag = 0;
-
-
- if(!flag)
- {
- Do computation
- If(computaion fails)
- flag = 0;
- }
-
-
- ...
-
- if(flag)
- Clean up and print error messages and do the relevant stuff and return
- else
- dealloc and return
-
-
- This must be a common scenario for all of us. I have been under the impression
- that "goto" might make the code a look like a puzzle and I religiously
- followed the rule "Never goto GOTO". This made my code very complex.
- Often it lead to leaks.
- I would return in the middle of a function forgetting to cleanup only to be
- detected by testcenter. This also results in code redundancy.
-
- What does everyone do?? I have always been tempted to use goto especially in
- functions involving a lot of computation and error checking.
- I have heard people say "People who don't know how to code use GOTO". I was
- curious to know how guys that say this solve the above problem.
-
- Cheers,
- Srini.
-
-
-
-
-
-